import request from '@/axios'
import * as cacheApi from '@/utils/cache'

function formatRequestUri(name) {
  return 'form/form/' + name
}
export function Query(query, paging) {
  return request.post({
    url: formatRequestUri('Query'),
    data: {
      Query: query,
      Index: paging.Index,
      Size: paging.Size,
      SortName: paging.SortName,
      IsDesc: paging.IsDesc
    }
  })
}

export function Add(data) {
  return request.post({
    url: formatRequestUri('Add'),
    data
  })
}
export function Set(id, data) {
  return request.post({
    url: formatRequestUri('Set'),
    data: {
      Id: id,
      Value: data
    }
  })
}
export function Get(id) {
  return request.get({
    url: formatRequestUri('Get'),
    params: {
      id
    }
  })
}

export function Delete(id) {
  return request.get({
    url: formatRequestUri('Delete'),
    params: {
      id
    }
  })
}

export function Enable(id) {
  return request.get({
    url: formatRequestUri('Enable'),
    params: {
      id
    }
  })
}

export function Disable(id) {
  return request.get({
    url: formatRequestUri('Stop'),
    params: {
      id
    }
  })
}

export function GetBody(id) {
  return request.get({
    url: formatRequestUri('GetBody'),
    params: {
      id
    }
  })
}

export function GetStructure(param) {
  const key = getKey('formBody_' + param.FormId, param)
  return cacheApi.agentRequest(key, () => {
    return request.post({
      url: formatRequestUri('GetStructure'),
      data: param
    })
  })
}
export function getKey(key, param) {
  if (param.TableId && param.TableId.length > 0) {
    key = key + '_' + param.TableId.join(',')
  }
  if (param.FuncType && param.FuncType.length > 0) {
    key = key + '_' + param.FuncType.join(',')
  }
  if (param.DataType && param.DataType.length > 0) {
    key = key + '_' + param.DataType.join(',')
  }
  if (param.ShowMode && param.ShowMode.length > 0) {
    key = key + '_' + param.ShowMode.join(',')
  }
  if (param.IsSystem) {
    key = key + '_' + param.IsSystem
  }
  return key
}

export function GetStructures(param) {
  const key = getKey('formBodys_' + param.FormId.join(','), param)
  return cacheApi.agentRequest(key, () => {
    return request.post({
      url: formatRequestUri('GetStructures'),
      data: param
    })
  })
}
export function SyncPhyBody(id) {
  return request.get({
    url: formatRequestUri('SyncPhyBody'),
    params: {
      id
    }
  })
}
export function GetOutline(formId, pageType) {
  return request.get({
    url: formatRequestUri('GetOutline'),
    params: {
      formId,
      pageType
    }
  })
}

export function SetLayoutType(id, layout) {
  return request.post({
    url: formatRequestUri('SetLayoutType'),
    data: {
      Id: id,
      Value: layout
    }
  })
}

export function GetTable(formId) {
  return request.get({
    url: formatRequestUri('GetTable'),
    params: {
      formId
    }
  })
}

export function GetColumns(data) {
  return request.post({
    url: formatRequestUri('GetColumns'),
    data
  })
}
